<?php

namespace services;

require 'library/define.php';

require_once ROOT_LIBRARY_RESTFUL . DS . 'restful.php';
require_once ROOT_LIBRARY_DB . DS . 'PDOWrapper.php';
require_once ROOT_LIBRARY_APC . DS . 'fileCache.php';
require_once ROOT_LIBRARY_APC . DS . 'wrapperCache.php';
require_once ROOT_LIBRARY_LAZADA . DS . 'global_vars.php';

require_once ROOT_SERVICES . DS . 'service_interface.php';

class service extends \restful {

    public function __construct() {
        parent::__construct();
        $this->_init_resource();
    }

    public function router() {
        if ($this->_verify() == FALSE) {
            $func = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : null;
            if (isset($this->_request['format']) && in_array($this->_request['format'], array('json', 'html', 'xml'))) {
                $this->_content_type = 'application/' . $this->_request['format'];
            }
            if ((int) method_exists($this, $func) > 0) {
                $this->$func();
            } else {
                $this->response('', 404);
            }
        } else {
            $this->response(json_encode(array('result' => 'error', 'data' => 'Invalid API Key')), 404);
        }
    }

    private function _verify() {
        $sign = isset($this->_request['sign']) ? $this->_request['sign'] : null;
        if (isset($sign)) {
            $sku = isset($this->_request['sku']) ? $this->_request['sku'] : null;
            return ($sign === md5($sku . API_KEY));
        }
        return FALSE;
    }

    private function _init_resource() {
        $config = include ROOT_CONFIG;
        $local_db = isset($this->_request['country']) ? $this->_request['country'] : 'vn';

        $db = \PDOWrapper::instance();
        $db->configMaster($config['db'][$local_db]['master']);
        $cache = \wrapperCache::GetInstance($config['apc_cache']['cache_type'], $config['apc_cache']['cache_expire'], $config['apc_cache']['cache_path']);
        
        $global = \global_vars::instance();
        $global::set('db', $db);
        $global::set('cache', $cache);
    }

    private function get_stock() {
        try {
            $sku = isset($this->_request['sku']) ? $this->_request['sku'] : null;
            if (isset($sku)) {
                $global = \global_vars::instance();
                \services\service_interface::set_resource($global::get('db'), $global::get('cache'));
                $content = \services\service_interface::get_stock($sku);
                $this->response(json_encode($content));
            } else {
                $this->response(json_encode(array('result' => 'error', 'data' => 'no parameter')), 404);
            }
        } catch (\PDOException $pe) {
            $this->response(json_encode(array('result' => 'error', 'data' => 'PDO')));
        } catch (Exception $e) {
            $this->response(json_encode(array('result' => 'error', 'data' => 'EXCE')));
        }
    }
    
     private function get_status_shipping() {
        try {
            $sku = isset($this->_request['sku']) ? $this->_request['sku'] : null;
            if (isset($sku)) {
                $global = \global_vars::instance();
                \services\service_interface::set_resource($global::get('db'), $global::get('cache'));
                $content = \services\service_interface::get_status_shipping($sku);
                $this->response(json_encode($content));
            } else {
                $this->response(json_encode(array('result' => 'error', 'data' => 'no parameter')), 404);
            }
        } catch (\PDOException $pe) {
            $this->response(json_encode(array('result' => 'error', 'data' => '')));
        } catch (Exception $e) {
            $this->response(json_encode(array('result' => 'error', 'data' => '')));
        }
    }

}


//error_reporting(0);
$service = new service();
$service->router();
?>


